package net.adetech.cjg.innerservice.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.cjg.innerservice.entity.Ywdl;
import net.adetech.cjg.innerservice.entity.Ywlx;
import net.adetech.cjg.innerservice.entity.Zd;
import net.adetech.cjg.innerservice.entity.Zdpc;
import net.adetech.cjg.innerservice.mapper.ZdDao;
import net.adetech.cjg.innerservice.mapper.ZdpcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class YwlxController {
    
    @Autowired
    ZdpcDao zdpcDao;

    @Autowired
    private HttpServletRequest request;


    private static Map<String, Ywlx> jdcYwlxMap = new LinkedHashMap<>();
    private static Map<String, Ywlx> jszYwlxMap = new LinkedHashMap<>();
    private static Map<String, Ywdl> jdcYwdlMap = new LinkedHashMap<>();

    static {
        jdcYwlxMap.put("1",new Ywlx("1","申请6年免检","","",false,"0"));
        jdcYwlxMap.put("2",new Ywlx("2","补领号牌","","",true,"1"));

        jdcYwlxMap.put("3",new Ywlx("3","换领号牌","","",true,"1"));

        jdcYwlxMap.put("16",new Ywlx("16","互换号牌号码","","",false,"1"));
        jdcYwlxMap.put("4",new Ywlx("4","补领行驶证","","",true,"1"));
        jdcYwlxMap.put("5",new Ywlx("5","换领行驶证","","",true,"1"));
        jdcYwlxMap.put("15",new Ywlx("15","补牌+补证","","",false,"1"));
        jdcYwlxMap.put("6",new Ywlx("6","申领登记证书","","",false,"1"));
        jdcYwlxMap.put("7",new Ywlx("7","补领登记证书","","",false,"1"));
        jdcYwlxMap.put("8",new Ywlx("8","换领登记证书","","",false,"1"));
        jdcYwlxMap.put("9",new Ywlx("9","申请检验合格标志","","",false,"1"));
        jdcYwlxMap.put("10",new Ywlx("10","补领检验合格标志","","",false,"1"));
        jdcYwlxMap.put("11",new Ywlx("11","换领检验合格标志","","",false,"1"));
        jdcYwlxMap.put("201",new Ywlx("201","注册登记","","",false,"2"));
        jdcYwlxMap.put("202",new Ywlx("202","注销登记","","",false,"2"));
        jdcYwlxMap.put("203",new Ywlx("203","转移登记","","",false,"2"));
        jdcYwlxMap.put("204",new Ywlx("204","车辆转入","","",false,"2"));
        jdcYwlxMap.put("205",new Ywlx("205","车辆转出","","",false,"2"));
        jdcYwlxMap.put("301",new Ywlx("301","变更登记/备案","","",false,"3"));
        jdcYwlxMap.put("401",new Ywlx("401","抵押登记","","",false,"4"));
        jdcYwlxMap.put("402",new Ywlx("402","解除抵押登记","","",false,"4"));
        jdcYwlxMap.put("403",new Ywlx("403","质押","","",false,"4"));
        jdcYwlxMap.put("404",new Ywlx("404","解除质押","","",false,"4"));
        jdcYwlxMap.put("901",new Ywlx("901","修改机动车联系方式","","",false,"9"));

        jszYwlxMap.put("1",new Ywlx("1","首次申领","","",false,null));
        jszYwlxMap.put("99",new Ywlx("99","修改驾驶人联系方式","","",true,null));
        jszYwlxMap.put("21",new Ywlx("21","变更考试地","","",false,null));
        jszYwlxMap.put("2",new Ywlx("2","增加准驾车型","","",false,null));
        jszYwlxMap.put("3",new Ywlx("3","持军警驾驶证申领","","",false,null));
        jszYwlxMap.put("4",new Ywlx("4","持境外驾驶证申领","","",false,null));
        jszYwlxMap.put("5",new Ywlx("5","证件损毁换证","C","G",true,null));
        jszYwlxMap.put("6",new Ywlx("6","转入换证","C","B",true,null));
        jszYwlxMap.put("7",new Ywlx("7","有效期满换证","C","A",true,null));
        jszYwlxMap.put("67",new Ywlx("67","转入换证(期满)","C","AB",true,null));
        jszYwlxMap.put("613",new Ywlx("613","转入换证(遗失)","C","BF",true,null));
        jszYwlxMap.put("8",new Ywlx("8","达到规定年龄换证","","",false,null));
        jszYwlxMap.put("9",new Ywlx("9","自愿降低准驾车型换证","","",false,null));
        jszYwlxMap.put("10",new Ywlx("10","因身体条件变化降低准驾车型换证","","",false,null));
        jszYwlxMap.put("11",new Ywlx("11","信息变化换证","","",false,null));
        jszYwlxMap.put("12",new Ywlx("12","信息备案","","",false,null));
        jszYwlxMap.put("13",new Ywlx("13","补证","C","F",true,null));
        jszYwlxMap.put("14",new Ywlx("14","注销","","",false,null));
        jszYwlxMap.put("15",new Ywlx("15","注销最高准驾车型","","",false,null));
        jszYwlxMap.put("16",new Ywlx("16","注销实习准驾车型","","",false,null));
        jszYwlxMap.put("17",new Ywlx("17","恢复驾驶资格","","",false,null));
        jszYwlxMap.put("18",new Ywlx("18","延期换证","","",false,null));
        jszYwlxMap.put("19",new Ywlx("19","延期审验","","",false,null));
        jszYwlxMap.put("20",new Ywlx("20","延期提交身体条件证明","","",false,null));
        jszYwlxMap.put("31",new Ywlx("31","申请校车驾驶资格","","",false,null));
        jszYwlxMap.put("32",new Ywlx("32","注销校车驾驶资格","","",false,null));

        jdcYwdlMap.put("9",new Ywdl("9","修改机动车联系方式",true));
        jdcYwdlMap.put("0",new Ywdl("0","申请6年免检",true));
        jdcYwdlMap.put("1",new Ywdl("1","机动车牌证业务",false));
        jdcYwdlMap.put("2",new Ywdl("2","机动车注册/转移/注销/转入",false));
        jdcYwdlMap.put("3",new Ywdl("3","机动车变更登记/备案",false));
        jdcYwdlMap.put("4",new Ywdl("4","机动车抵押/质押",false));

    }











    @RequestMapping(value = "/cjg/ywlx", produces = "application/json")
    public String getYwlx()
    {

        List<Ywlx> jdcYwlxList = new LinkedList<>();
        List<Ywdl> jdcYwdlList = new LinkedList<>();
        List<Ywlx> jszYwlxList = new LinkedList<>();


        String ip  = getClientIp();
        Zdpc zdpc= zdpcDao.getZdpcByIp(ip);

        if (zdpc.getQxlx()==null||"1".equals(zdpc.getQxlx())){
            for (Ywlx ywlx:jdcYwlxMap.values()
            ) {
                jdcYwlxList.add(ywlx);
            }

            for (Ywdl ywdl:jdcYwdlMap.values())
            {
                jdcYwdlList.add(ywdl);
            }

            for (Ywlx ywlx:jszYwlxMap.values()
            ) {
                jszYwlxList.add(ywlx);
            }
        }
        else {

            String[] jdcYwlxArray = zdpc.getJdcqx().split(",");
            List<String> ywdlKeyList = new ArrayList<>();
            for (String key:jdcYwlxArray)
            {
                Ywlx ywlx = jdcYwlxMap.get(key);
                jdcYwlxList.add(ywlx);
                if (!ywdlKeyList.contains(ywlx.getFilter()))
                {
                    Ywdl ywdl = jdcYwdlMap.get(ywlx.getFilter());
                    jdcYwdlList.add(ywdl);
                    ywdlKeyList.add(ywlx.getFilter());
                }
            }


            String[] jszYwlxArray = zdpc.getJszqx().split(",");
            for (String key:jszYwlxArray)
            {
                Ywlx ywlx = jszYwlxMap.get(key);
                jszYwlxList.add(ywlx);

            }
            
            
        }

        JSONObject resultJson = new JSONObject();
        JSONArray jdcYwlxArray = JSONObject.parseArray(JSONObject.toJSONString(jdcYwlxList));
        JSONArray jdcYwdlArray = JSONObject.parseArray(JSONObject.toJSONString(jdcYwdlList));
        JSONArray jszYwlxArray = JSONObject.parseArray(JSONObject.toJSONString(jszYwlxList));


        resultJson.put("jdcYwlxArray",jdcYwlxArray);
        resultJson.put("jdcYwdlArray",jdcYwdlArray);
        resultJson.put("jszYwlxArray",jszYwlxArray);


        return resultJson.toJSONString();
    }


    private String getClientIp() {
        String remoteAddr = "";

        if (request != null) {
            remoteAddr = request.getHeader("X-FORWARDED-FOR");
            if (remoteAddr == null || "".equals(remoteAddr)) {
                remoteAddr = request.getRemoteAddr();
            }
        }

        return remoteAddr;
    }

}
